package com.frejoys.common.config;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
public class AliPayConfigManager {

   private final AliPayProperties properties;

   private final ValueConfig valueConfig;

   @PostConstruct
   private void init() {

       Config config = new Config();
       config.protocol = "https";
       config.gatewayHost = properties.getGateway();
       config.signType = "RSA2";
       config.appId = properties.getAppid();

       config.encryptKey = properties.getAes();

       // 应用私钥
       config.merchantPrivateKey = properties.getPrivateKey();

       // 应用公钥
       // config.merchantCertPath = properties.getAppCertPublicKey();
       config.merchantCertPath = "cert/appCertPublicKey_2021004145654722.crt";
       // 支付宝公钥
       // config.alipayCertPath = properties.getAlipayCertPublicKey();
       config.alipayCertPath = "cert/alipayCertPublicKey_RSA2.crt";
       // 支付宝根证书
       // config.alipayRootCertPath = properties.getAlipayRootCert();
       config.alipayRootCertPath = "cert/alipayRootCert.crt";

       // 非证书模式
       // config.alipayPublicKey = properties.getPublicKey();


       config.notifyUrl = valueConfig.getAppUrl() + properties.getNotifyUrl();

       Factory.setOptions(config);
   }

}
